package com.xudu.tourbackend.controller.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xudu.tourbackend.common.DeleteBatchRequest;
import com.xudu.tourbackend.common.PageRequest;
import com.xudu.tourbackend.common.Result;
import com.xudu.tourbackend.common.globalEnum.ErrorCode;
import com.xudu.tourbackend.controller.RouteController;
import com.xudu.tourbackend.entity.request.AddRouteRequest;
import com.xudu.tourbackend.entity.request.UpdateRouteRequest;
import com.xudu.tourbackend.entity.vo.RouteVo;
import com.xudu.tourbackend.exception.ServiceException;
import com.xudu.tourbackend.service.IRouteService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @className: RouteControllerImpl
 * @description: TODO
 * @author: xudu
 * @create: 2024-06-17
 */
@CrossOrigin(origins = "*")
@RestController
public class RouteControllerImpl implements RouteController {

    @Resource
    private IRouteService routeService;

    @Override
    public Result addRoute(@Valid AddRouteRequest addRouteRequest, HttpServletRequest request) {
        if (addRouteRequest == null){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }
        RouteVo routeVo = routeService.addRoute(addRouteRequest, request);
        return Result.success(routeVo);
    }

    @Override
    public Result searchRouteById(int id, HttpServletRequest request) {
        if (id <= 0){
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        RouteVo routeVo = routeService.searchRoute(id, request);
        return Result.success(routeVo);
    }

    @Override
    public Result searchRouteByName(String routeName, HttpServletRequest request) {
        //此处参数不用判空，业务层模糊查询
        IPage<RouteVo> routeVoIPage = routeService.searchRoute(routeName, request);
        //PageRequest pageRequest = new PageRequest();
        //PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        //PageInfo<RouteVo> pageInfo = new PageInfo<>(routeVos);

        return Result.success(routeVoIPage);
    }

    @Override
    public Result searchRouteByElement(Integer elementId) {
        List<RouteVo> routeVos = routeService.searchRoute(elementId);
        if (routeVos.isEmpty()){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }
        PageRequest pageRequest = new PageRequest();
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        PageInfo<RouteVo> pageInfo = new PageInfo<>(routeVos);
        return Result.success(pageInfo);
    }

    @Override
    public Result searchRouteByIdForMiniprogram(int id) {
        if (id <= 0){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }
        RouteVo routeVo = routeService.searchRoute(id);
        return Result.success(routeVo);
    }

    @Override
    public Result updateRoute(UpdateRouteRequest updateRouteRequest, HttpServletRequest request) {
        Integer id = updateRouteRequest.getId();
        if (id == null || id <= 0){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }
        RouteVo routeVo = routeService.updateRoute(updateRouteRequest, request);
        return Result.success(routeVo);
    }

    @Override
    public Result deleteRoute(@RequestBody DeleteBatchRequest deleteBatchRequest, HttpServletRequest request) {
        List<Integer> ids = deleteBatchRequest.getIds();
        int i = routeService.deleteRoute(ids, request);
        return Result.success(i);
    }

    @Override
    public Result auditRoute(int id, HttpServletRequest request) {
        if (id <= 0){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }
        Boolean b = routeService.auditRoute(id, request);
        return Result.success(b);
    }
}
